package com.hanlink.dsp.ca.model.settings;

import com.hanlink.dsp.BtDspApplication;
import com.hanlink.dsp.DspSettings;
import com.hanlink.dsp.ca.model.channel.XoverChannel;
import com.hanlink.dsp.model.BaseSettings;
import com.hanlink.dsp.protocols.DeviceProtocol;
import com.hanlink.dsp.protocols.XoverProtocol;
import com.qztech.bluetooth.BluetoothClientManager;
import com.qztech.utils.YzDebug;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *
 *  BTDSP
 *
 *
 *  Copyright (C) 秦智科技有限责任公司版权所有: 2015/10/11
 *
 * Description:
 *
 * Revision History
 * Date			         Author		  Description
 * 2015/10/11        fonhal       初始创建
 *
 * </pre>
 */
@Table(name = "_xover_settings")
public class XoverSettings extends BaseSettings {

    @Column(name = "last_channel")
    private int lastChannel = 0;//最后设置的Channel


    @Column(name = "settings_id")
    private int settingsId;

    private final   List<XoverChannel> mXoverChannels;

    @Column(name = "base_ch1")
    private int baseCh1;
    @Column(name = "base_ch2")
    private int baseCh2;
    @Column(name = "base_ch3")
    private int baseCh3;
    @Column(name = "base_ch4")
    private int baseCh4;
    @Column(name = "base_ch5")
    private int baseCh5;
    @Column(name = "base_ch6")
    private int baseCh6;
    @Column(name = "base_ch7")
    private int baseCh7;
    @Column(name = "base_ch8")
    private int baseCh8;

    public XoverSettings(){
        super();
        mXoverChannels = new ArrayList<>();
    }
    @Override
    public void resetData() {
        lastChannel = 0;
        synchronized (mXoverChannels){
            for (XoverChannel channel : mXoverChannels) {
                channel.resetData();
            }
        }
        setGroupInfo(new int[8]);
    }
    public void getChannels(DbManager db) throws DbException {
        List<XoverChannel> xoverChannels = null;
        if (db != null) {
            try {
                xoverChannels = db.selector(XoverChannel.class).where("settings_id", "=", this.id).orderBy("channel").findAll();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        YzDebug.d("getChannels xoverChannels: "+ (xoverChannels == null ?" empty":(" size:"+ xoverChannels.size())));
        if (xoverChannels == null || xoverChannels.size() <= 0) {
            xoverChannels = new ArrayList<>();
            for (int i = 0; i < DspSettings.CHANNEL_LENGTH; i++) {
                xoverChannels.add(new XoverChannel(i));
            }
        }
        this.setXoverChannels(xoverChannels);
    }



    public List<XoverChannel> getXoverChannels() {
        return mXoverChannels;
    }


    public XoverChannel getCurrentChannel() {
        return mXoverChannels.get(lastChannel);
    }

    public void setXoverChannels(List<XoverChannel> xoverChannels) {
        synchronized (mXoverChannels){
            mXoverChannels.clear();
            mXoverChannels.addAll(xoverChannels);
        }
        for (XoverChannel channel : mXoverChannels) {
            channel.setSettingsId(this.id);
        }
    }

    public int getLastChannel() {
        return lastChannel;
    }

    public void setLastChannel(int lastChannel) {
        this.lastChannel = lastChannel;
    }

    public int getSettingsId() {
        return settingsId;
    }

    public void setSettingsId(int settingsId) {
        this.settingsId = settingsId;
    }

    public int getBaseCh1() {
        return baseCh1;
    }

    public void setBaseCh1(int baseCh1) {
        this.baseCh1 = baseCh1;
    }

    public int getBaseCh2() {
        return baseCh2;
    }

    public void setBaseCh2(int baseCh2) {
        this.baseCh2 = baseCh2;
    }

    public int getBaseCh3() {
        return baseCh3;
    }

    public void setBaseCh3(int baseCh3) {
        this.baseCh3 = baseCh3;
    }

    public int getBaseCh4() {
        return baseCh4;
    }

    public void setBaseCh4(int baseCh4) {
        this.baseCh4 = baseCh4;
    }

    public int getBaseCh5() {
        return baseCh5;
    }

    public void setBaseCh5(int baseCh5) {
        this.baseCh5 = baseCh5;
    }

    public int getBaseCh6() {
        return baseCh6;
    }

    public void setBaseCh6(int baseCh6) {
        this.baseCh6 = baseCh6;
    }

    public int getBaseCh7() {
        return baseCh7;
    }

    public void setBaseCh7(int baseCh7) {
        this.baseCh7 = baseCh7;
    }

    public int getBaseCh8() {
        return baseCh8;
    }

    public void setBaseCh8(int baseCh8) {
        this.baseCh8 = baseCh8;
    }

    public boolean isGroup12() {
        return baseCh1 == 1 || baseCh2 == 1;
    }

    public boolean isGroup34() {
        return baseCh3 == 1 || baseCh4 == 1;
    }


    public boolean isGroup56() {
        return baseCh5 == 1 || baseCh6 == 1;
    }


    public boolean isGroup78() {
        return baseCh7 == 1 || baseCh8 == 1;
    }

    public boolean isGroup1234() {
        return baseCh1 == 11 || baseCh2 == 11 || baseCh3 == 11 || baseCh4 == 11;
    }
    public boolean isGroup5678() {
        return baseCh5 == 11 || baseCh6 == 11 || baseCh7 == 11 || baseCh8 == 11;
    }

    public boolean isGroupAll() {
        return  baseCh1 == 1111 ||
                baseCh2 == 1111 ||
                baseCh3 == 1111 ||
                baseCh4 == 1111 ||
                baseCh5 == 1111 ||
                baseCh6 == 1111 ||
                baseCh7 == 1111 ||
                baseCh8 == 1111;
    }
    public int[] getGroupInfo(){
        return new int[]{
                baseCh1,baseCh2,baseCh3,baseCh4,baseCh5,baseCh6,baseCh7,baseCh8
        };
    }

    public void setGroupInfo(int[] info){
        if (info[0] != -1) baseCh1 = info[0];
        if (info[1] != -1) baseCh2 = info[1];
        if (info[2] != -1) baseCh3 = info[2];
        if (info[3] != -1) baseCh4 = info[3];
        if (info[4] != -1) baseCh5 = info[4];
        if (info[5] != -1) baseCh6 = info[5];
        if (info[6] != -1) baseCh7 = info[6];
        if (info[7] != -1) baseCh8 = info[7];
    }
    public   int[] getGroupChannel(int channel) {
        if (isGroupAll()){
            return new int[]{0, 1, 2, 3,4,5};
        }else {
            switch (channel){
                case 0:
                case 1:
                    if (isGroup12()){
                        return new int[]{0,1};
                    }
                    break;
                case 2:
                case 3:
                    if (isGroup34()){
                        return new int[]{2,3};
                    }
                    break;
                case 4:
                case 5:
                    if (isGroup56()){
                        return new int[]{4,5};
                    }
                    break;
                case 6:
                case 7:
                    if (isGroup78())
                        return new int[]{6,7};
                    break;
            }
        }

        return new int[]{channel};
    }

    @Override
    public String toString() {
        return "XoverSettings{" +
                "lastChannel=" + lastChannel +
                ", settingsId=" + settingsId +
                ", channel=" + mXoverChannels +
                '}';
    }

    public void resetHighPass() {
        int index = DspSettings.XOVER_SETTINGS.getLastChannel();
        List<XoverChannel> xoverChannels = getXoverChannels();
        for (XoverChannel xoverChannel : xoverChannels) {
            xoverChannel.resetHighPass();
        }
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(DeviceProtocol.resetXoverHighPass());
        XoverProtocol.send(xoverChannels.get(index), 1 ,true);
    }

    public void resetLowPass() {
        int index = DspSettings.XOVER_SETTINGS.getLastChannel();
        List<XoverChannel> xoverChannels = getXoverChannels();
        for (XoverChannel xoverChannel : xoverChannels) {
            xoverChannel.resetLowPass();
        }
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(DeviceProtocol.resetXoverLowPass());
        XoverProtocol.send(xoverChannels.get(index), 2, true);
    }

    public void reset() {
        BluetoothClientManager.getInstance(BtDspApplication.getInstance()).write(DeviceProtocol.resetXover());
    }


    public XoverSettings copy(){
        final XoverSettings xs = new XoverSettings();
        xs.id = id;
        xs.refId = refId;
        xs.lastChannel = lastChannel;
        xs.settingsId = settingsId;
        xs.baseCh1 = baseCh1;
        xs.baseCh2 = baseCh2;
        xs.baseCh3 = baseCh3;
        xs.baseCh4 = baseCh4;
        xs.baseCh5 = baseCh5;
        xs.baseCh6 = baseCh6;
        xs.baseCh7 = baseCh7;
        xs.baseCh8 = baseCh8;
        for (XoverChannel channel : mXoverChannels) {
            xs.mXoverChannels.add(channel.copy());
        }

        return xs;
    }

    public void copyIdFromSrc(XoverSettings xoverSettings) {
        this.id = xoverSettings.id;
        this.refId = xoverSettings.refId;
        for (XoverChannel dst : mXoverChannels) {
            for (XoverChannel src : xoverSettings.mXoverChannels) {
                if (src.getChannel() == dst.getChannel()){
                    dst.setId(dst.getId());
                    dst.setSettingsId(src.getSettingsId());
                }
            }
        }
    }
}
